﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using produccion.clases;
using produccion.forms;

namespace produccion.clases
{
    public class CLSDETSAL
    {
        MDOgeneral mod = new MDOgeneral();
        string tipsal_dsa, hojnro_dsa, codmat_dsa, codcol_dsa;
        double canpro_dsa, cospro_dsa, canstn_dsa;

        public string TIPSAL_DSA
        {
            get { return tipsal_dsa; }
            set { tipsal_dsa = value; }
        }
        public string HOJNRO_DSA
        {
            get { return hojnro_dsa; }
            set { hojnro_dsa = value; }
        }
        public string CODMAT_DSA
        {
            get { return codmat_dsa; }
            set { codmat_dsa = value; }
        }
        public string CODCOL_DSA
        {
            get { return codcol_dsa; }
            set { codcol_dsa = value; }
        }
        public double CANPRO_DSA
        {
            get { return canpro_dsa; }
            set { canpro_dsa = value; }
        }
        public double COSPRO_DSA
        {
            get { return cospro_dsa; }
            set { cospro_dsa = value; }
        }
        public double CANSTN_DSA
        {
            get { return canstn_dsa; }
            set { canstn_dsa = value; }
        }

        public DataTable Imprimir()
        {
            try
            {
                string query = "SELECT TIPSAL_DSA, HOJNRO_DSA, CODMAT_DSA, CODCOL_DSA, CANPRO_DSA, COSPRO_DSA, CANSTN_DSA, NOMMAT_MAT, NOMCOL_COL FROM DETSALP INNER JOIN MAEMAT ON CODMAT_DSA=CODMAT_MAT INNER JOIN MAECOLP ON CODCOL_DSA=CODCOL_COL WHERE TIPSAL_DSA='" + TIPSAL_DSA + "' AND HOJNRO_DSA='" + HOJNRO_DSA + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return mod.consulta(query);
                }
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update detsalp set canpro_dsa=" + CANPRO_DSA + ", cospro_dsa=" + COSPRO_DSA + ", canstn_dsa=" + CANSTN_DSA + " where tipsal_dsa='" + TIPSAL_DSA + "' and hojnro_dsa='" + HOJNRO_DSA + "' and codmat_dsa='" + CODMAT_DSA + "' and codcol_dsa='" + CODCOL_DSA + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Boolean ExisteRegistro()
        {
            try
            {
                string query = "select tipsal_dsa from detsalp where tipsal_dsa='" + TIPSAL_DSA + "' and hojnro_dsa='" + HOJNRO_DSA + "' and codmat_dsa='" + CODMAT_DSA + "' and codcol_dsa='" + CODCOL_DSA + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insertar()
        {
            try
            {
                string query = "";
                query = "select * from detsalp where tipsal_dsa = '" + TIPSAL_DSA + "' and hojnro_dsa = '" + HOJNRO_DSA + "' and codmat_dsa = '" + CODMAT_DSA + "' and codcol_dsa = '" + CODCOL_DSA + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    query = "update detsalp set canpro_dsa = canpro_dsa + " + CANPRO_DSA + " where tipsal_dsa = '" + TIPSAL_DSA + "' and hojnro_dsa = '" + HOJNRO_DSA + "' and codmat_dsa = '" + CODMAT_DSA + "' and codcol_dsa = '" + CODCOL_DSA + "'";
                    return mod.comando(query);
                }
                else
                {
                    if (String.IsNullOrEmpty(HOJNRO_DSA) == true)
                    {
                        HOJNRO_DSA = " ";
                    }

                    query = "insert into detsalp (tipsal_dsa,hojnro_dsa,codmat_dsa,codcol_dsa,canpro_dsa,cospro_dsa,canstn_dsa) values ('" + TIPSAL_DSA + "','" + HOJNRO_DSA + "','" + CODMAT_DSA + "','" + CODCOL_DSA + "','" + CANPRO_DSA + "','" + COSPRO_DSA + "','" + CANSTN_DSA + "')";
                    return mod.comando(query);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public double Existencia()
        {
            try
            {
                string query = "Select (ExiMtp_mtp - cansep_mtp) as cantidad from maemtp where codmat_mtp='" + CODMAT_DSA + "' and codcol_mtp='" + CODCOL_DSA + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    if (System.Convert.ToDouble(mod.consulta(query).Rows[0][0].ToString()) > 0)
                    {
                        return System.Convert.ToDouble(mod.consulta(query).Rows[0][0].ToString());
                    }
                    else
                    {
                        return 0;
                    }
                }
                return 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from detsalp where tipsal_dsa='" + TIPSAL_DSA + "' and hojnro_dsa='" + HOJNRO_DSA + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string UniComMP(string CODMAT_DPP)
        {
            try
            {
                DataTable dt = new DataTable();
                string query = "select nomuni_eun from maemat inner join encunip on unicom_mat=coduni_eun where codmat_mat='" + CODMAT_DPP + "'";
                dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    return dt.Rows[0][0].ToString();
                }
                return "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Consultar()
        {
            try
            {
                DataTable dt = new DataTable();
                string query = "select tipsal_dsa, hojnro_dsa, codmat_dsa, codcol_dsa, canpro_dsa, cospro_dsa, canstn_dsa, nommat_mat from detsalp inner join maemat on codmat_dsa=codmat_mat where tipsal_dsa='" + TIPSAL_DSA + "' and hojnro_dsa='" + HOJNRO_DSA + "'";
                dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {                    
                    return dt;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable rImpSalMat()
        {
            try 
            {
                string query = "select codmat_dsa as CODIGO, (select trim(nommat_mat) from maemat where codmat_mat=codmat_dsa) as DESCRIPCION, (select trim(nomcol_col) from maecolp where codcol_col=codcol_dsa) as COLOR, (select trim(nomuni_eun) from maemat, encunip where coduni_eun=unicom_mat and codmat_mat=codmat_dsa) as UNIDAD, cospro_dsa as COSTO, canpro_dsa as CANTIDAD, (cospro_dsa * canpro_dsa) as SUBTOTAL from detsalp where tipsal_dsa='"+TIPSAL_DSA+"' and  hojnro_dsa='"+HOJNRO_DSA+"'";
                return mod.consulta(query);
            }
            catch (Exception ex){throw ex;}
        }
    }
}
